package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfTrends;
import com.shms.bean.IndexParam;
import com.shms.dao.ShTrendsDao;
import com.shms.entity.ShTrends;
import com.shms.until.PageUntil;

/**
 * 文件名：ShTrendsDaoImpl.java
 * 编写者：陆遥
 * 创建时间：2013-1-3
 * 简要描述：
 * 动态信息
 * 与数据库处理交互方法
 */
public class ShTrendsDaoImpl extends HibernateDaoSupport implements ShTrendsDao {
	@SuppressWarnings({ "finally", "unchecked" })
	@Override
	public List<ShTrends> queryTrendsByExample(IndexParam indexParam) {
		@SuppressWarnings("rawtypes")
		List<ShTrends> list = new ArrayList();
		/*
		 * 执行的代码，其中可能有异常。一旦发现异常，则立即跳到catch执行。否则不会执行catch里面的内容 
		 * 除非try里面执行代码发生了异常，否则这里的代码不会执行 
		 * 不管什么情况都会执行，包括try catch 里面用了return ,可以理解为只要执行了try或者catch，就一定会执行 finally 
		 */
		try {
			StringBuffer strBuf = new StringBuffer();
			strBuf.append(" SELECT * FROM ");
			strBuf.append(" (SELECT * FROM SH_TRENDS ORDER BY TTIME DESC) ");
			strBuf.append(" WHERE ROWNUM <= :param ");
			strBuf.append(" ORDER BY ROWNUM ");
			//获取SessionFactory,进而获取CurrentSession创建SQLQuery
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(strBuf.toString());
			//为SQL语句绑定参数
			sqlQuery.setProperties(indexParam);
			list = sqlQuery.addEntity(ShTrends.class).list();
		} catch (Exception ex) {
			//提示异常信息，输出到控制台
			ex.printStackTrace();
		} finally {
			return list;
		}
	}
	
	@SuppressWarnings("finally")
	public GroupOfTrends queryTrends(GroupOfTrends groupOfTrends) {
		GroupOfTrends gOfTrendsRtn = new GroupOfTrends();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_TRENDS";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfTrends.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfTrends.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfTrends.getPageSize(), currentPage);
			
			sql = "SELECT * FROM SH_TRENDS ORDER BY TTIME DESC";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfTrends.getPageSize());
			@SuppressWarnings("unchecked")
			//将查询的结果封装到ShTrends实体类中
			List<ShTrends> trendsListRtn = sqlQuery.addEntity(ShTrends.class).list();
			gOfTrendsRtn = new GroupOfTrends(currentPage, totalPage, allRow, trendsListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfTrendsRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean saveTrends(ShTrends shTrends) {
		//保存是否成功的标志
		boolean rtn = true;
		try {
			//保存数据的方法
			this.getSessionFactory().getCurrentSession().save(shTrends);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean updateTrends(ShTrends shTrends) {
		//更新是否成功的标志
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shTrends);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			String sql = "DELETE FROM SH_TRENDS T WHERE T.TID IN (:ids)";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置参数
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public ShTrends queryById(ShTrends shTrends) {
		ShTrends shTrendsRtn = new ShTrends();
		try {
			String sql = "SELECT * FROM SH_TRENDS WHERE TID = :tid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shTrends);
			@SuppressWarnings("unchecked")
			List<ShTrends> listTrends = sqlQuery.addEntity(ShTrends.class).list();
			if (listTrends.size() == 1) {
				shTrendsRtn = listTrends.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shTrendsRtn;
		}
	}
}
